// 按需加载

import Vue from 'vue'
import Router from 'vue-router'
import SectionNav from '@/components/common/SectionNav'
import Index from '@/view/index/Index'
import Event from '@/view/event/Event'
import Video from '@/view/video/Video'
import Finance from '@/view/finance/Finance'
import Pic from '@/view/pic/Pic'
import { MENU, SEARCH, SQUARE } from '@/../static/const'
import { mapState } from 'vuex'
import store from "@/store/store";
Vue.use(Router)
const router = new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      redirect: '/index',
      meta: {
        singleSection: {
          is: false, type: MENU, title: ''
        }
      }
    },
    // 页面：默认首页
    {
      // 这个路由没有继续往下匹配，为什么
      // path:'/:title/:title?'这样才能匹配到第二级,
      // 难道默认是exact?
      path: '/index',
      component: Index,
      children: [
        {
          path: '',
          components: { sectionTop: SectionNav },
          meta: {
            singleSection: {
              is: false, type: MENU, title: ''
            }
          }
        }
      ]
    },
    // 页面：视频页面
    {
      path: '/video',
      component: Video,
      children: [
        {
          path: '',
          components: { sectionTop: SectionNav },
          meta: {
            singleSection: {
              is: false, type: MENU, title: ''
            }
          }
        }
      ]
    },
    // 页面：时事页面
    {
      path: '/event',
      component: Event,
      children: [
        {
          path: '',
          components: { sectionTop: SectionNav },
          meta: {
            singleSection: {
              is: false, type: MENU, title: ''
            }
          }
        }
      ]
    },
    // 页面：财经页面
    {
      path: '/finance',
      component: Finance,
      children: [
        {
          path: '',
          components: { sectionTop: SectionNav },
          meta: {
            singleSection: {
              is: false, type: MENU, title: ''
            }
          }
        }
      ]
    },
    // 页面：图片页面
    {
      path: '/section/pic',
      component: Pic,
      children: [
        {
          path: '',
          components: { sectionTop: SectionNav },
          meta: {
            singleSection: {
              is: true, type: MENU, title: '图片'
            }
          }
        }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  console.log(to)
  // var { singleSection } = {
  //   ...mapState({
  //     singleSection: state => state.singleSection
  //   })
  // }
  var singleSection = store.state.singleSection
  var meta = to.meta.singleSection
  console.log(singleSection)
  if (meta.is !== singleSection.is) {
    store.commit('HEADER_CHANGE', meta)
    next()
  } if (meta.type !== singleSection.type) {
    store.commit('HEADER_CHANGE', meta)
    next()
  }
  // 需要调用next，不然会拦截
  next()
})
export default router
